﻿$import( "SmitearWebGL.Programs.AbstractProgram" );

SmitearWebGL.Programs.LightPrePass.Terrain.NormalProgram = SmitearWebGL.Programs.AbstractProgram.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Programs.LightPrePass.Terrain.NormalProgram" );
	}
	,
	
	/******** public method ********/
	
	activeUniform : function( renderableObject )
	{
		renderableObject.glUniform( SmitearWebGL.ShaderUniform.projectionMatrix, this._shader );
		renderableObject.glUniform( SmitearWebGL.ShaderUniform.worldViewMatrix, this._shader );
		renderableObject.glUniform( SmitearWebGL.ShaderUniform.normalViewMatrix, this._shader );
		
		renderableObject.getMaterial().glUniform( SmitearWebGL.ShaderUniform.shininess, this._shader );
		
		return SmitearWebGL.Core.HResult.SUCCESS;
	}
	,
	
	/******** private method ********/
	
	_generateVertexShaderStr : function()
	{
		this._super();
		
		var vertStr = [];
		vertStr.push( "uniform mat4 uWorldViewMatrix;\n" );
		vertStr.push( "uniform mat4 uNormalViewMatrix;\n" );
		vertStr.push( "uniform mat4 uProjectionMatrix;\n" );
		vertStr.push( "attribute vec2 " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION0 + ";\n" );
		vertStr.push( "attribute float " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION1 + ";\n" );
		vertStr.push( "attribute vec3 " + SmitearWebGL.Resources.Buffer.AttribLocation.NORMAL +";\n" );
		vertStr.push( "varying vec3 vNormal;\n" );
		vertStr.push( "void main(void)\n" );
		vertStr.push( "{\n" );
		vertStr.push( "vec4 combinedPos = vec4(" + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION0 + ".x, " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION1 + ", " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION0 + ".y, 1.0 );\n" );
		vertStr.push( "gl_Position = uProjectionMatrix * uWorldViewMatrix * combinedPos;\n" );
		vertStr.push( "vNormal = normalize((uNormalViewMatrix * vec4(" + SmitearWebGL.Resources.Buffer.AttribLocation.NORMAL +", 0.0)).xyz);\n" );
		vertStr.push( "}\n" );
		
		return vertStr.join( "" );
	}
	,
	_generateFragmentShaderStr : function()
	{
		this._super();
		
		var fragStr = [];
		fragStr.push( "#ifdef GL_ES\n" );
		fragStr.push( "precision highp float;\n" );
		fragStr.push( "#endif\n" );
		fragStr.push( "varying vec3 vNormal;\n" );
		fragStr.push( "uniform float uShininess;\n" );
		fragStr.push( "void main(void)\n" );
		fragStr.push( "{\n" );
		fragStr.push( "vec3 n  = (normalize(vNormal) + 1.0) / 2.0;\n" );
		fragStr.push( "gl_FragColor = vec4(n, uShininess);\n" );
		fragStr.push( "}\n" );
		
		return fragStr.join( "" );
	}
}
);